package com.edu.hre.struts.action.admin;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreLanguage;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.struts.form.admin.EducationUpdateForm;
import com.edu.hre.struts.form.admin.LanguageUpdateForm;

public class LanguageUpdateAction extends DispatchAction{
	
	private static final Logger logger = Logger.getLogger(LanguageUpdateAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private final String ACTION_NAME = "languageUpdate";
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		LanguageUpdateForm beanForm = (LanguageUpdateForm) form;
		beanForm.setIsAddNew(true);

		if(request.getParameter(StringConstants.BTN_ADD) == null){
			beanForm.setMessage(StringConstants.BLANK);
			return mapping.findForward(ACTION_NAME);
		}
		
		String message = validateForm(beanForm);
		if(!StringUtils.isBlank(message)){
			beanForm.setMessage(message);
			return mapping.findForward(ACTION_NAME);
		}
		
		HreLanguage tmp = new HreLanguage();
		tmp.setName(CommonUtils.normalizeString(beanForm.getName()));
		tmp.setDateCreated(new Date());
		tmp.setDateModified(new Date());

		boolean status = baseService.saveOrUpdate(tmp);
		if (!status) {
			beanForm.setMessage(StringConstants.INSERT_FAILURE_MESSAGE);
			return mapping.findForward(ACTION_NAME);
		}

		response.getWriter().write(StringConstants.POP_UP_CLOSE_SCRIPT);
		return null;
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		LanguageUpdateForm beanForm = (LanguageUpdateForm) form;
		beanForm.setIsAddNew(false); 
		
		if(request.getParameter(StringConstants.BTN_EDIT) == null){

			int id = CommonUtils.getIntParameter("id", request);
			HreLanguage tmp = baseService.getPojo(id, HreLanguage.class);
			
			if (tmp == null) {
				beanForm.setMessage(StringConstants.BLANK);
				return mapping.findForward(ACTION_NAME);
			}
			
			beanForm.setId(id);
			beanForm.setName(CommonUtils.normalizeString(tmp.getName()));
			beanForm.setMessage(StringConstants.BLANK);
			return mapping.findForward(ACTION_NAME);
		}
		
		String message = validateForm(beanForm);
		if(!StringUtils.isBlank(message)){
			beanForm.setMessage(message);
			return mapping.findForward(ACTION_NAME);
		}
		
		HreLanguage tmp = baseService.getPojo(beanForm.getId(), HreLanguage.class);
		if (tmp == null) {
			beanForm.setMessage(StringConstants.BLANK);
			return mapping.findForward(ACTION_NAME);
		}

		tmp.setId(beanForm.getId());
		tmp.setName(CommonUtils.normalizeString(beanForm.getName()));
		tmp.setDateModified(new Date());

		boolean status = baseService.saveOrUpdate(tmp);
		if (!status) {
			beanForm.setMessage(StringConstants.UPDATE_FAILURE_MESSAGE);
			return mapping.findForward(ACTION_NAME);
		}

		response.getWriter().write(StringConstants.POP_UP_CLOSE_SCRIPT);
		return null;
	}
	
	private String validateForm (LanguageUpdateForm beanForm) {
		
		if(StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getName()))){
			return "Language name cannot be blank!";
		}

		return StringConstants.BLANK;
	}
}
